home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / codelib9 / v_11_05 / test_obj / invoice.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-01  |  2.3 KB  |  96 lines

  1. // File: invoice.cpp
  2. // Copyright Norman Wilde 1993
  3. // Code for the invoice example
  4. #include "invoice.h"
  5. #include <stdio.h>
  6. ostream& operator << (ostream& s, Item& i) {
  7.   s << "Item - code: " << i.prodCode
  8.     << " quant: " << i.quant
  9.     << " basePrice: " << i.basePrice;
  10.   return s;
  11. }
  12. Item::Item(long aCode, long aQuant, float aPrice ) {
  13.   prodCode = aCode;
  14.   quant = aQuant;
  15.   basePrice = aPrice;
  16. }
  17. float Item::value(){
  18.   return quant * basePrice;
  19. }
  20. long Item::code(){
  21.   return prodCode;
  22. }
  23. ItemList::ItemList(Item * anItem ) {
  24.   theItem = anItem;
  25.   nextItem = NULL;
  26. }
  27. void ItemList::addItem(Item * anItem ) {
  28.   ItemList * newList = new ItemList(anItem);
  29.   ItemList *i;
  30.   for(i = this; ; i = i->next()) {
  31.     if( NULL == i->nextItem) {
  32.       i->nextItem = newList;
  33.       break;
  34.     }
  35.   }
  36. }
  37. Item * ItemList::currentItem() {
  38.   return theItem;
  39. }
  40. ItemList * ItemList::next() {
  41.   return nextItem;
  42. }
  43. ItemList::~ItemList() {
  44.   if (nextItem != NULL)
  45.     delete nextItem;
  46.   delete theItem;
  47. }
  48. ostream& operator << (ostream& s, Client& c) {
  49.   s << "Client - cType: ";
  50.   if(RETAIL == c.cType) s << "RETAIL";
  51.   if(WHOLESALE == c.cType) s << "WHOLESALE";
  52.   if(FOREIGN == c.cType) s << "FOREIGN";
  53.   return s;
  54. }
  55. Client::Client(ClientType aClientType) {
  56.   cType = aClientType;
  57. }
  58. ClientType Client::type() {
  59.   return cType;
  60. }
  61. ostream& operator << (ostream&s, Invoice& i) {
  62.   s << "Invoice - client: " << * i.iClient << "\n"
  63.     << " items: \n";
  64.   for(ItemList *iL = i.iList; NULL != iL; iL = iL->next())
  65.     s << *(iL->currentItem()) << "\n";
  66.   return s;
  67. }
  68. Invoice::Invoice(Client *aClient) {
  69.   iClient = aClient;
  70.   iList = NULL;
  71. }
  72. void Invoice::addItem(Item *anItem) {
  73.   if( NULL == iList)
  74.     iList = new ItemList(anItem);
  75.   else
  76.     iList->addItem(anItem);
  77. }
  78. float Invoice::totalDiscount(){
  79.   switch (iClient->type()) {
  80.     case RETAIL: return 0.0;
  81.     case WHOLESALE: {
  82.       float disc = 0.0;
  83.       for ( ItemList *i = iList; i != NULL; i = i->next())
  84.         disc += 0.1 * (i->currentItem())->value();
  85.       return disc;
  86.     }
  87.     case FOREIGN: {
  88.       float disc = 0.0;
  89.       for ( ItemList *i = iList; i != NULL; i = i->next())
  90.         if((i->currentItem())->code() > 1000)
  91.           disc += 0.1 * (i->currentItem())->value();
  92.       return disc;
  93.     }
  94.   }
  95. }
  96.